package com.etone.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.etone.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.etone.common.annotation.Log;
import com.etone.common.core.controller.BaseController;
import com.etone.common.core.domain.AjaxResult;
import com.etone.common.enums.BusinessType;
import com.etone.system.domain.FiveCtuRecord;
import com.etone.system.service.IFiveCtuRecordService;
import com.etone.common.utils.poi.ExcelUtil;
import com.etone.common.core.page.TableDataInfo;

/**
 * CTU库存信息Controller
 *
 * @author etone
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/system/record")
public class FiveCtuRecordController extends BaseController
{
    @Autowired
    private IFiveCtuRecordService fiveCtuRecordService;

    /**
     * 查询CTU库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(FiveCtuRecord fiveCtuRecord)
    {
        startPage();
        List<FiveCtuRecord> list = fiveCtuRecordService.selectFiveCtuRecordList(fiveCtuRecord);
        return getDataTable(list);
    }

    /**
     * 导出CTU库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "CTU库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FiveCtuRecord fiveCtuRecord)
    {
        List<FiveCtuRecord> list = fiveCtuRecordService.selectFiveCtuRecordList(fiveCtuRecord);
        ExcelUtil<FiveCtuRecord> util = new ExcelUtil<FiveCtuRecord>(FiveCtuRecord.class);
        util.exportExcel(response, list, "CTU库存信息数据");
    }

    /**
     * 获取CTU库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fiveCtuRecordService.selectFiveCtuRecordById(id));
    }

    /**
     * 新增CTU库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "CTU库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FiveCtuRecord fiveCtuRecord)
    {
        return toAjax(fiveCtuRecordService.insertFiveCtuRecord(fiveCtuRecord));
    }

    /**
     * 修改CTU库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "CTU库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FiveCtuRecord fiveCtuRecord)
    {
        return toAjax(fiveCtuRecordService.updateFiveCtuRecord(fiveCtuRecord));
    }

    /**
     * 删除CTU库存信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "CTU库存信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fiveCtuRecordService.deleteFiveCtuRecordByIds(ids));
    }
    /**
     * 通过物料号或者仓位号进行查询
     * */
    @GetMapping("/gainRecordList")
    @Anonymous
    public AjaxResult gainRecordList(FiveCtuRecord fiveCtuRecord)
    {
        String wuliaoCode = fiveCtuRecord.getWuliaoCode();
        List<Map<String,Object>>  mapList = new ArrayList<>();
        fiveCtuRecord.setWuliaoCodelike(wuliaoCode);
        fiveCtuRecord.setWuliaoCode(null);
        List<FiveCtuRecord> list = fiveCtuRecordService.selectFiveCtuRecordList(fiveCtuRecord);
        for(FiveCtuRecord fiveCtuRecord1 : list){
            Map<String,Object> map = new HashMap<>();
            map.put("id",fiveCtuRecord1.getId());
            map.put("wuliaoNo",fiveCtuRecord1.getWuliaoCode());
            //610001C1501013
            String cangweiNo = fiveCtuRecord1.getStorageName();
            String pai = cangweiNo.substring(1,2)+"排";
            String lie = cangweiNo.substring(4,6)+"列";
            String ceng = cangweiNo.substring(10,11)+"层";
            map.put("cangweiNo",pai+lie+ceng);
            map.put("isRequest",fiveCtuRecord1.getIsRequest());
            mapList.add(map);
        }
        return AjaxResult.success(mapList);
    }
}
